package com.yang.controller;

import com.yang.entity.User;
import com.yang.entity.UserList;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/hello")
public class HelloHandler {

    @PostMapping(value = "/index", params = "num")
    public String index(@RequestParam("num") Integer id) {
        System.out.println("接收到了请求");
        System.out.println(id);
        //返回逻辑视图
        return "hello";
    }

    @RequestMapping("/index/{id}/{name}")
    public String restful(@PathVariable("id") Integer id, @PathVariable("name") String name) {
        System.out.println(id + "-" + name);
        return "index";
    }

    @RequestMapping("/cookie")
    public String getCookie(@CookieValue("JSESSIONID") String sessionId) {
        System.out.println(sessionId);
        return "index";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(User user) {
        System.out.println(user);
        return "index";
    }

    @RequestMapping("/baseType")
    @ResponseBody
    public String baseType(int id) {
        return "id:" + id;
    }

    @RequestMapping("/packageType")
    @ResponseBody
    public String packageType(Integer id) {
        return "id:" + id;
    }

    @RequestMapping("/arrayType")
    @ResponseBody
    public String arrayType(String[] names) {
        StringBuffer stringBuffer = new StringBuffer();
        for (String s : names) {
            stringBuffer.append(s).append(" ");
        }

        return stringBuffer.toString();
    }

    @RequestMapping(value = "/adds", method = RequestMethod.POST)
    @ResponseBody
    public String addUser(User user) {
        System.out.println(user);
        return user.toString();
    }

    @RequestMapping(value = "/listType")
    @ResponseBody
    public String listType(UserList userList) {
        StringBuffer stringBuffer = new StringBuffer();
        for (User user : userList.getUserList()) {
            stringBuffer.append(user);
        }
        return stringBuffer.toString();
    }

    @RequestMapping("/jsonType")
    @ResponseBody
    public User jsonType(@RequestBody User user) {
        return user;
    }
}
